package com.example.rg1broadcastreceiverdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button buttonSend;
    private NetWorkStateReceiver netWorkStateReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonSend=findViewById(R.id.buttonSend);
        buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建意图
                Intent intent=new Intent();
                //意图名称
                intent.setAction("com.example.broadcast.MY_BROADCAST");
                //广播数据
                intent.putExtra("msg","Knowledge starts with practice.");
                //标准广播
                sendBroadcast(intent);
                //有序广播
                //sendOrderedBroadcast(intent,null);
            }
        });

        netWorkStateReceiver = new NetWorkStateReceiver();
        IntentFilter networkFilter = new IntentFilter();
        networkFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(netWorkStateReceiver,networkFilter);
    }

    @Override
    protected void onDestroy() {
        //注销
        unregisterReceiver(netWorkStateReceiver);
        super.onDestroy();
    }
}
